package com.nidu.demo.file.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.file.convertor.FileConvertor;
import com.nidu.demo.file.convertor.FileCriteriaConvertor;
import com.nidu.demo.file.dto.FileCO;
import com.nidu.demo.file.dto.FilePageQry;
import com.nidu.demo.file.gateway.FileGateway;
import com.nidu.demo.file.model.File;
import com.nidu.demo.file.model.FileCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 文件分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class FilePageQryExe {

    private final FileGateway fileGateway;
    private final FileConvertor fileConvertor;
    private final FileCriteriaConvertor fileCriteriaConvertor;

    /**
     * 分页查询文件
     *
     * @param qry 分页查询条件
     * @return 分页结果
     */
    public PageResponse<FileCO> execute(FilePageQry qry) {
        // 1. 转换查询条件
        FileCriteria criteria = fileCriteriaConvertor.toDataObject(qry);
        
        // 2. 执行分页查询
        PageResponse<File> pageResponse = fileGateway.pageByCondition(criteria);
        
        // 3. 转换为DTO并返回
        return PageResponse.of(
                fileConvertor.toClientObjects(pageResponse.getData()),
                pageResponse.getTotalCount(),
                pageResponse.getPageSize(),
                pageResponse.getPageIndex()
        );
    }
}
